﻿using Hr.BLL;
using Hr.Model;
using HR.HrHelper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.Serialization.Configuration;
using System.Web;
using System.Web.Http;

namespace HR.Areas.letao.Controllers.Menu
{
    public class TypeMangeController : ApiController
    {
        private MenuService MenuService = new MenuService();
        int code = 0;
        string msg = "success";

        /// <summary>
        /// 获取所有类型数据，包括模糊查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JObject GetTypeListAll(string Term)
        {
            if (Term == null)
            {
                Term = "";
            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",MenuService.GetTypeListAll(Term)}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 获取审核人信息，分页查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public JObject GetAuditList(int page,int limit,string Term)
        {
            IEnumerable<object> AL = MenuService.GetAuditList(page, limit, Term).Where(n => n.Key.Equals("AuditList")).FirstOrDefault().Value as IEnumerable<object>;
            int Count =int.Parse(MenuService.GetAuditList(page, limit, Term).Where(n => n.Key.Equals("Count")).FirstOrDefault().Value.ToString());
            List<Object> L = new List<object>();
            foreach (var item in AL)
            {
                string path = item.GetType().GetProperty("ImagePath1").GetValue(item).ToString();
                Dictionary<string, object> it = new Dictionary<string, object>()
                {
                    {"ImagePath1", FileHelper.GetImgInfo(path)},
                    {"UserName",item.GetType().GetProperty("UserName").GetValue(item).ToString() },
                    {"FullName", item.GetType().GetProperty("FullName").GetValue(item).ToString()},
                    {"Phone1",item.GetType().GetProperty("Phone1").GetValue(item).ToString() },
                    {"RoleName_CHS", item.GetType().GetProperty("RoleName_CHS").GetValue(item).ToString()},
                    {"DepartmentName", item.GetType().GetProperty("DepartmentName").GetValue(item).ToString()},
                    {"pk_User",item.GetType().GetProperty("pk_User").GetValue(item).ToString() },
                };
                L.Add(it);
            }
            
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",new{ AuditList=L,Count=Count} }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 根据类型ID获取类型信息
        /// </summary>
        /// <param name="pk_TypeID"></param>
        /// <returns></returns>
        [HttpGet]
        public JObject GetTypeInfo(int pk_TypeID)
        {
            Sys_TypeID T = MenuService.GetTypeInfo(pk_TypeID);
            string name = "";
            List<User_List> UserList = new List<User_List>();
            T.Description_CHT= T.Description_CHT == null ? "" : T.Description_CHT;
            if (T.Description_CHT.ToString().Length>0)
            {
                if(T.Description_CHT.IndexOf("|") != -1)
                {
                    foreach (int item in T.Description_CHT.Split('|').Select(n => int.Parse(n)).ToArray())
                    {
                        User_List U = MenuService.GetUserInfo(item);
                        if (U != null)
                        {
                            name += U.FullName + ",";
                            UserList.Add(U);
                        }
                    }
                    if (name.IndexOf(",") != -1)
                    {
                        name = name.Substring(0, name.Length - 1);
                    }
                   
                }
                else
                {
                    User_List U = MenuService.GetUserInfo(int.Parse(T.Description_CHT.ToString()));
                    if (U != null)
                    {
                        name = U.FullName;
                        UserList.Add(U);
                    }
                    
                }
            }
            T.Description_CHT = name;
            if (T == null)
            {
                code = 1;
                msg = "没有类型信息";
            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",T },
                {"UserList",UserList }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 添加、修改类型
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JObject UpdateType()
        {
            System.Collections.Specialized.NameValueCollection RequestForm = HttpContext.Current.Request.Form;
            int pk_User = int.Parse(RequestForm["pk_User"].ToString());
            Sys_TypeID ST = JsonHelper.DeserializeJsonToObject<Sys_TypeID>(RequestForm["TypeInfo"]);
            string Type = RequestForm["Type"];

            if (Type.Equals("Add"))
            {
                msg = "添加成功";
                if (!MenuService.UpdateType(ST, Type, pk_User))
                {
                    code = 1;
                    msg = "添加失败";
                }
            }else if (Type.Equals("Edit"))
            {
                msg = "修改成功";
                if (!MenuService.UpdateType(ST, Type, pk_User))
                {
                    code = 1;
                    msg = "修改失败";
                }
            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 删除类型
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JObject DelType()
        {
            System.Collections.Specialized.NameValueCollection RequestForm = HttpContext.Current.Request.Form;
            int pk_TypeID = int.Parse(RequestForm["pk_TypeID"].ToString());

            Sys_TypeID T = MenuService.GetTypeInfo(pk_TypeID);
            if (T != null)
            {
                msg = "删除成功";
                if (!MenuService.DelType(T))
                {
                    code = 1;
                    msg = "删除失败";
                }
            }
            else
            {
                code = 1;
                msg = "类型不存在";
            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
    }
}
